package com.woniuxy.oldmanapi.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.cvo.OverdueVo;
import com.woniuxy.oldmanapi.cvo.WarehouseVo;
import com.woniuxy.oldmanapi.service.OverdueService;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class OverdueController {
    @Autowired
    OverdueService overdueService;

    //查询出库单，以及出库单明细
    @GetMapping("/overdue/searchList")
    public ResponseResult<PageInfo<OverdueVo>> list(String outname,
                                                    @RequestParam(value = "state", defaultValue = "-1") int state,
                                                    @RequestParam(value = "size", defaultValue = "6") int size,
                                                    @RequestParam(value = "now", defaultValue = "1") int now) {

        ResponseResult<PageInfo<OverdueVo>> result = new ResponseResult<>();
        try {
            PageInfo<OverdueVo> pageInfo = overdueService.findOverdueVosByCons(outname, state, size, now);
            result.setMsg("查询成功");
            result.setCode(200);
            result.setData(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("查询失败");
            result.setCode(201);

        }
        return result;
    }

    //添加==药品过期单
    @PostMapping("/overdue/add")
    public ResponseResult<Void> addOverdue(@RequestBody OverdueVo overdueVo) {

        ResponseResult<Void> result = new ResponseResult<>();
        try {
            overdueService.addOverdue(overdueVo);
            result.setMsg("添加成功");
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("添加失败");
            result.setCode(201);
        }
        return result;
    }

    //编辑==药品出库单
    @PostMapping("/overdue/modify")
    public ResponseResult<Void> modifyOverdue(@RequestBody OverdueVo overdueVo) {

        ResponseResult<Void> result = new ResponseResult<>();
        try {
            overdueService.modifyOverdue(overdueVo);
            result.setMsg("修改成功");
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("修改失败");
            result.setCode(201);
        }
        return result;
    }

    //撤销
    @GetMapping("/overdue/cancel/{id}")
    public ResponseResult<Void> cancel(@PathVariable("id") int id) {

        ResponseResult<Void> result = new ResponseResult<>();
        try {
                overdueService.cancelOverdue(id);
            result.setMsg("撤销成功");
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("撤销失败");
            result.setCode(201);

        }
        return result;
    }

    //出库
    @PostMapping("/overdue/reduceOverdue")
    public ResponseResult<Void> putWarehouse(@RequestBody OverdueVo overdueVo) {

        ResponseResult<Void> result = new ResponseResult<>();
        try {
            overdueService.putOverdue(overdueVo);
            result.setMsg("出库成功");
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("出库失败");
            result.setCode(201);

        }
        return result;
    }

}
